package com.acce.gxsc.page.outCar.detail;

import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.NetInBatchListBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.ToastUtils;

import rx.Subscriber;

import static com.acce.gxsc.common.Constants.OUT_CAR_GATE_NO;
import static com.acce.gxsc.common.Constants.SUPERVISION_GATE_NO;

/**
 * Created by huxley on 2017/5/26.
 */
public class OutCarDetailPresenter extends BasePresenter<OutCarDetailView> {

    public void loadOutCarDetail(String feeId) {
        WorkModel.getInstance().loadBatchList(feeId).subscribe(new Subscriber<ResultBean<NetInBatchListBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中", false);
                }
            }

            @Override
            public void onNext(ResultBean<NetInBatchListBean> datas) {
                if (mViewRef != null) {
                    if (!"200".equals(datas.returnCode)) {
                        ToastUtils.show(datas.returnMsg);
                        return;
                    }
                    getView().loadInCarListSuccess(datas.result);
                }
            }

            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void toOutCar(String inCarId, String outCarRemarks) {
        if (!Tools.verificationId(OUT_CAR_GATE_NO)) {
            return;
        }
        WorkModel.getInstance().toOutCar(inCarId, outCarRemarks).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("离场中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    ToastUtils.show(datas.returnMsg);
                    if ("200".equals(datas.returnCode)) {
                        getView().toOutCarSuccess();
                    }
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("处理失败！");
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void supervisionCar(String inCarId, String superviseRemarks) {
        if (!Tools.verificationId(SUPERVISION_GATE_NO)) {
            return;
        }
        WorkModel.getInstance().supervisionCar(inCarId, superviseRemarks).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("监察中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    ToastUtils.show(datas.returnMsg);
                    if ("200".equals(datas.returnCode)) {
                        getView().supervisionCarSuccess();
                    }
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("处理失败！");
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
